package com.example.egcar1.controller;

import com.example.egcar1.base.response.JsonResult;
import com.example.egcar1.base.response.StatusCode;
import com.example.egcar1.mapper.VehicleMapper;
import com.example.egcar1.pojo.dto.VehicleAddParam;
import com.example.egcar1.pojo.dto.VehicleListQuery;
import com.example.egcar1.pojo.entity.Vehicle;
import com.example.egcar1.pojo.vo.UserLoginVo;
import com.example.egcar1.pojo.vo.VehicleListVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController("/v1/vehicle")
@Api(tags = "汽车管理模块")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;
    @PostMapping("/add")
    @ApiOperation("添加车")
    @ApiOperationSupport(order = 10)
    public JsonResult insertVehicle(@RequestBody VehicleAddParam vehicleAddParam,
                                    @ApiIgnore HttpSession session) {
        log.debug("vehicleAddParam=" + vehicleAddParam);
        UserLoginVo userLoginVo = (UserLoginVo)session.getAttribute("user");
        if(userLoginVo==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Vehicle vehicle = new Vehicle();

        BeanUtils.copyProperties(vehicleAddParam, vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId(userLoginVo.getId());
        vehicleMapper.insertVehicle(vehicle);
        return JsonResult.ok();
    }
    @GetMapping("/list")
    @ApiOperation("获取汽车列表")
    @ApiOperationSupport(order = 20)
    public JsonResult getVehicleList(VehicleListQuery vehicleListQuery,
                                     @ApiIgnore HttpSession session){
        log.debug("vehicleListQuery="+vehicleListQuery);
        UserLoginVo userLoginVo = (UserLoginVo)session.getAttribute("user");
        if(userLoginVo==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        List<VehicleListVo> vehicleList = vehicleMapper.selectVehicleList(vehicleListQuery);
        return JsonResult.ok(vehicleList);
    }
}
